package com.example.administrator.x2picturetransliteration.Home.Adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseListViewAdapter;
import com.example.administrator.x2picturetransliteration.Bean.CheckBean;
import com.example.administrator.x2picturetransliteration.R;

import java.util.List;

/**
 * 检查更新
 * Created by Administrator on 2019/8/27.
 */

public class ToUpdateAdapter extends BaseListViewAdapter{

    private Context context;
    private List<CheckBean> mData;

    public ToUpdateAdapter(Context context,List<CheckBean> mData) {
        super(context);

        this.context = context;
        this.mData = mData;
    }

    @Override
    public int getLayoutView() {
        return R.layout.home_item_to_update;
    }

    @Override
    public int mCount() {
        return mData.size();
    }

    @Override
    public void logic(ViewHolder mViewHolder, View view, final int position) {

        IViewHolder mIViewHolder = (IViewHolder)mViewHolder;

        mIViewHolder.bbhText.setText(mData.get(position).getAppVersion());

        mIViewHolder.gxText.setVisibility(mData.get(position).getUrl()!=null?View.VISIBLE:View.GONE);

        mIViewHolder.titleText.setText(mData.get(position).getUrl()!=null?"最新版本":"当前版本");

        mIViewHolder.gxText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mData.get(position).getUrl()!=null)downloadNewApk(mData.get(position).getUrl());
            }
        });
    }

    @Override
    public ViewHolder InstantiationView(View mview) {

        IViewHolder mIViewHolder = new IViewHolder();
        mIViewHolder.bbhText = mview.findViewById(R.id.bbhText);
        mIViewHolder.gxText = mview.findViewById(R.id.gxText);
        mIViewHolder.titleText = mview.findViewById(R.id.titleText);

        return mIViewHolder;
    }

    private class IViewHolder extends ViewHolder{

        TextView bbhText; //展示版本号
        TextView gxText; //展示更新按钮
        TextView titleText;
    }

    private void downloadNewApk(String path) {
        Uri uri = Uri.parse(path);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }
}
